using Acme.BookStore.Car;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp;
using Volo.Abp.Domain.Repositories;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;

namespace Acme.BookStore.Car
{
    public class OutStockOrderAppService : CrudAppService<
        OrderOutStock, OutStockOrderDto, int, PagedAndSortedResultRequestDto, CreateUpdateOutStockOrderDto>, IOutStockOrderAppService
    {
        private readonly IRepository<InventryTran, int> _inventoryTransferRepository;
        private readonly IRepository<Merchandise, int> _merchandiseRepository;
        private readonly IRepository<OrderOutDetail, int> _outStockOrderDetailRepository;

        public OutStockOrderAppService(
            IRepository<OrderOutStock, int> repository,
            IRepository<InventryTran, int> inventoryTransferRepository,
            IRepository<Merchandise, int> merchandiseRepository,
            IRepository<OrderOutDetail, int> outStockOrderDetailRepository
        ) : base(repository)
        {
            _inventoryTransferRepository = inventoryTransferRepository;
            _merchandiseRepository = merchandiseRepository;
            _outStockOrderDetailRepository = outStockOrderDetailRepository;
        }

        /// <summary>
        /// 根据调拨单生成出库单及明细（不直接保存，返回前端编辑）
        /// </summary>
        public async Task<OutStockOrderDtoWithDetails> CreateFromInventoryTransferAsync(int inventoryTransferId)
        {
            // 1. 查询调拨单主表
            var transfer = await _inventoryTransferRepository.GetAsync(inventoryTransferId);
            if (transfer == null)
                throw new UserFriendlyException("调拨单不存在");

            // 2. 查询调拨明细
            var transferDetails = await _merchandiseRepository.GetListAsync(x => x.InventoryTranId == inventoryTransferId);

            // 3. 组装出库单主表
            var outStockOrder = new OutStockOrderDto
            {
                Id = transfer.Id, // 联查：用调拨单id
                InventoryTranId = transfer.Id,
                InventoryTransferType = transfer.InventoryTransferType,
                InventoryTransferNo = transfer.InventoryTransferNo,
                InventoryTransferOUT = transfer.InventoryTransferOUT,
                OutDateTime = System.DateTime.Now,
                InventoryTransferOperator = transfer.InventoryTransferOperator,
                Remarks = transfer.Remarks
            };

            // 4. 组装出库单明细
            var outStockOrderDetails = transferDetails.Select(d => new OutStockOrderDetailDto
            {
                Id = d.Id, // 联查：用调拨明细id
                OutOrderId = null, // 新建时无主表Id
                MerchandiseId = d.GoodsId,
                MerchandiseNo = d.MerchandiseNo,
                MerchandiseName = d.MerchandiseName,
                MerchandiseType = d.MerchandiseType,
                MerchandiseModel = d.MerchandiseModel,
                MerchandiseUnit = d.MerchandiseUnit,
                MerchandiseOut = d.MerchandiseOut,
                OutKuwei = null, // 由前端填写
                MerchandiseNumber = d.MerchandiseNumber,
                OutOrderNumber = null, // 由前端填写
                MerchandiseINPrice = d.MerchandiseINPrice,
                OutOrderPrice = null, // 由前端填写
                Reamres = d.Reamres
            }).ToList();

            return new OutStockOrderDtoWithDetails
            {
                OutStockOrder = outStockOrder,
                Details = outStockOrderDetails
            };
        }
    }
} 